<template>
  <ul class="user_overview flex" v-if="pageflag">
    <li class="user_overview-item" style="color: #00fdfa">
      <div class="user_overview_nums allnum ">
        <dv-digital-flop :config="powerConsumeCfg" style="width:100%;height:100%;" />
      </div>
      <p>耗电量</p>
    </li>
    <li class="user_overview-item" style="color: #07f7a8">
      <div class="user_overview_nums online">
        <dv-digital-flop :config="waterConsumeCfg" style="width:100%;height:100%;" />
      </div>
      <p>耗水量</p>
    </li>
    <li class="user_overview-item" style="color: #e3b337">
      <div class="user_overview_nums offline">
        <dv-digital-flop :config="carbonEmissionCfg" style="width:100%;height:100%;" />
      </div>
      <p>碳排放量</p>
    </li>
  </ul>
  <Reacquire v-else @onclick="getData" line-height="200px">
    重新获取
  </Reacquire>
</template>

<script setup>
//能耗总览,耗电量,耗水量,碳排放量
import { currentGET } from '@/api/modules'

const pageflag = ref(true)
const timer = ref(null)

const powerConsumeCfg = ref({
  number: [0],
  content: '{nt}',
  style: {
    fontSize: 24,
    fill: "#00fdfa",
  },
})
const waterConsumeCfg = ref({
  number: [0],
  content: '{nt}',
  style: {
    fontSize: 24,
    fill: "#07f7a8",
  },
})
const carbonEmissionCfg = ref({
  number: [0],
  content: '{nt}',
  style: {
    fontSize: 24,
    fill: "#e3b337",
  },
})

onMounted(()=>{
  getData()
})

// 获取报表数据
const getData = () => {
  pageflag.value = true;
  currentGET("big2").then((res) => {
    console.log(res)
    if (res.success) {
      let emissionOverview = res.rows[0];
      waterConsumeCfg.value.number = [emissionOverview.totalWaterConsume + emissionOverview.totalOfficeWaterConsume]
      powerConsumeCfg.value.number = [emissionOverview.totalPowerConsume + emissionOverview.totalOfficePowerConsume]
      carbonEmissionCfg.value.number = [emissionOverview.totalCarbonEmission]
      switper()
    } else {
      pageflag.value = false;
    }
  });
}

// 轮询
const switper = () => {
  if (timer.value) {
    return
  }
  let looper = (a) => {
    getData()
  };
  timer.value = setInterval(looper, 10000);
}

onBeforeUnmount(()=>{
  clearData()
})

// 清除数据
const clearData = () => {
  if (timer.value) {
    clearInterval(timer.value)
    timer.value = null
  }
}
</script>

<style lang='scss' scoped>
.user_overview {
  li {
    flex: 1;

    p {
      text-align: center;
      height: 16px;
      font-size: 16px;
    }

    .user_overview_nums {
      width: 160px;
      height: 160px;
      text-align: center;
      line-height: 100px;
      font-size: 12px;
      margin: 40px auto 30px;
      background-size: cover;
      background-position: center center;
      position: relative;

      &::before {
        content: "";
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        font-size: 5px;
        background-size: cover;
      }

      &.bgdonghua::before {
        animation: rotating 14s linear infinite;
      }
    }

    .allnum {
      &::before {
        background-image: url("../../assets/img/left_top_lan.png");
      }
    }

    .online {
      &::before {
        background-image: url("../../assets/img/left_top_lv.png");
      }
    }

    .offline {
      &::before {
        background-image: url("../../assets/img/left_top_huang.png");
      }
    }

    .laramnum {
      &::before {
        background-image: url("../../assets/img/left_top_hong.png");
      }
    } 
  }
}
</style>
